package com.kakaxi.mybilibili.utils;

import android.app.Activity;

import java.util.Stack;

/**
 * 活动栈管理器
 * 
 * Created by kakaxi on 16/8/15.
 */

public class ActivityStackManager {

    /** 活动堆栈管理器 **/
    private static ActivityStackManager mInstance;

    /** 活动栈列表 **/
    private Stack<Activity> mActivityStack = new Stack<>();

    private ActivityStackManager() {
    }

    /**
     * 获取单一管理器
     *
     * @return
     */
    public static synchronized ActivityStackManager getInstance() {
        if (mInstance == null) {
            mInstance = new ActivityStackManager();
        }
        return mInstance;
    }

    /**
     * 当前在栈顶的活动
     *
     * @return
     */
    public Activity currentActivity() {
        if (mActivityStack == null || mActivityStack.size() == 0) {
            return null;
        }
        return mActivityStack.peek();
    }

    /**
     * 把一个活动放入栈中
     *
     * @param activity 活动
     */
    public void pushActivity(Activity activity) {
        if (activity != null) {
            mActivityStack.push(activity);
        }
    }

    /**
     * 弹出栈顶的活动
     */
    public void popActivity() {
        if (mActivityStack.size() != 0) {
            LogUtil.d("弹出最顶层堆栈");
            Activity popActivity = mActivityStack.pop();
            popActivity.finish();
        }
    }

    /**
     * 弹出指定的活动
     *
     * @param activity 活动
     */
    public void popActivityWithoutFinish(Activity activity) {
        if (activity != null) {
            mActivityStack.remove(activity);
        }
    }

    /**
     * 弹出指定的活动
     *
     * @param activity 活动
     */
    public void popActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            mActivityStack.remove(activity);
        }
    }

    /**
     * 弹出指定的活动
     *
     * @param activityClass 活动类
     */
    public void popActivity(Class activityClass) {
        if (activityClass != null && mActivityStack.size() != 0) {
            for (int i = mActivityStack.size() - 1; i != -1; i--) {
                Activity activity = mActivityStack.get(i);
                if (activity.getClass().equals(activityClass)) {
                    activity.finish();
                    mActivityStack.remove(i);
                }
            }
        }
    }

    /**
     * 跳到指定的活动
     *
     * @param activityClass 活动类
     */
    public void popToActivity(Class activityClass) {
        if (activityClass != null) {
            while (true) {
                Activity activity = currentActivity();
                if (activity == null) {
                    break;
                }
                LogUtil.d("当前活动的class为" + activity.getClass().toString());
                if (activity.getClass().equals(activityClass)) {
                    LogUtil.d("跳出循环");
                    break;
                }
                popActivity();
            }
        }
    }

    /**
     * 跳到指定的活动
     *
     * @param activity 活动
     */
    public void popToActivity(Activity activity) {
        if (mActivityStack.size() != 0 && mActivityStack.contains(activity)) {
            while (true) {
                Activity currentActivity = currentActivity();
                if (activity == null) {
                    break;
                }
                if (currentActivity == activity) {
                    break;
                }
                popActivity();
            }
        }
    }

    /**
     * 弹出所有活动
     */
    public void popAllActivity() {
        while (true) {
            Activity activity = currentActivity();
            if (activity == null) {
                break;
            }
            popActivity();
        }
    }

}
